<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>万果园</title>
    <link href="file/images/file" rel="shortcut icon" type="image/x-icon"/>
    <meta name="keywords" content="优质，新鲜，美味"/>
    <meta name="description" content="为顾客题提供最优质的水果"/>
    <link href="file/css/detail.css" rel="stylesheet" type="text/css"/>
</head>
<body>
    <div class="top">
        <img src="file/images/dianmin.png" width="300px" height="100px">
        <div class="header-info">
            <div class="person-info">
                <a href="index.html">返回首页</a>
                <span>|</span>
                <a href="user/gwc.html">我的购物车</a>
                <span>|</span>
                <a href="user/perInfo.html">个人信息</a>
                <span>|</span>
                <a onclick="exitLogin()" style="cursor: pointer">退出登录</a>
            </div>
        </div>
    </div>
    <img id="pic" width="400" height="400" src=""> </br>

    <div class="shangpin">
        <h3 style="color:#f08c5a"><span id="gname">卫龙辣条</span></h3>
        商品价格: <span id="price">28</span>&nbsp;¥</br>
        商品计重单位: <span id="unit">包</span></br>
        商品描述: <span id="intro">卫龙辣条，好吃又实惠</span></br>
        商品介绍: <span id="descr">好吃</span></br>
        商品类别: <span id="tname">麻辣</span></br>
    </div>
    <div class="jiaru">
        <a><img src="file/images/gwc1.png" id="addGoods"
                style="cursor: pointer" width="200px" height="180px"></a>
    </div>

<script src="file/js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script>
    //网页加载 根据hash找到hash数据
    $(function () {
        let gno = location.hash.replace("#", "");	//替换'#' -> ''
        if (gno == "") {
            location.href = "index.html";
            return;
        }
        //通过gno来查询数据
        $.ajax({
            url: "/goods/" + gno,
            type: "GET",
            success: function (data) {
                if (data.code == -1) {
                    alert("对不起找不到商品");
                } else {
                    $("#gname").html(data.goodsinfo.gname);
                    $("#price").html(data.goodsinfo.price);
                    $("#unit").html(data.goodsinfo.unit);
                    $("#intro").html(data.goodsinfo.intro);
                    $("#descr").html(data.goodsinfo.descr);
                    $("#tname").html(data.goodsinfo.goodstype.tname);
                    $("#pic").attr("src", "file/images/goods/" + data.goodsinfo.pic)
                }
            }
        });
        //加入购物车
        $("#addGoods").click(function () {
            var ifAdd = confirm("是否添加商品到购物车中？");
            if (!ifAdd) {
                return;
            }

            $.ajax({
                url: "/addgoods/" + gno,
                type: "GET",
                success: function (data) {
                    console.log(data);
                    alert("添加购物车成功!");
                    location.href = "user/gwc.html";
                }
                ,error: function () {
                    alert("请先登录后再添加购物车!");
                    location.href = "loginPage.html";
                }
            })
        });
    })
    function exitLogin() {
        $.ajax({
            url: "/exitLogin" ,
            type: "GET",
            success: function () {
                location.href="index.html";
            }
        })
    };
</script>
</body>
</html>